package me.eleme.anubis.openapi.demo;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import me.eleme.anubis.openapi.api.exception.ServiceException;
import me.eleme.anubis.openapi.config.Config;
import me.eleme.anubis.openapi.config.oauth.response.Token;
import me.eleme.anubis.openapi.oauth.OauthClient;

/**
 * @author 刘志永
 */
@Slf4j
public class OauthClientDemo {
    /**
     * 设置是否沙箱环境
     */
    private static final boolean IS_SANDBOX = false;
    /**
     * 设置APP_ID
     */
    private static final String APP_ID = "app id";
    /**
     * 设置APP SECRET_KEY
     */
    private static final String SECRET_KEY = "secret key";

    /**
     * 初始化OAuthClient
     */
    private final static OauthClient CLIENT;

    private final static Config CONFIG;


    static {
        // 初始化全局配置工具
        CONFIG = new Config(IS_SANDBOX, APP_ID, SECRET_KEY);
        CLIENT = new OauthClient(CONFIG);
    }

    public static void main(String[] args) {
        OauthClientDemo demo = new OauthClientDemo();
        demo.serverTokenTest();
    }

    /**
     * 授权码获取Token
     */
    private void serverTokenTest() {
        String autoCode = "code";
        String redirectUri = "http://localhost:8899/demo";
        String merchantId = "87891";
        Token token = null;
        try {
            token = CLIENT.getTokenByCode(autoCode, merchantId,redirectUri);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        log.info("token:{}", JSON.toJSONString(token));
    }

    /**
     * 刷新Token
     */
    private void getTokenByRefreshTokenTest() {
        String refreshToken = "";
        String merchantId = "";
        Token token = null;
        try {
            token = CLIENT.getTokenByRefreshToken(merchantId,refreshToken);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        log.info("token:{}", JSON.toJSONString(token));
    }







}
